package com.google.code.eclipsercpspring.util;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class BundleClassLoaderXmlBeanFactory extends DefaultListableBeanFactory {
   private XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);

   public BundleClassLoaderXmlBeanFactory(Class contextClass, String resourcePath, BeanFactory parentFactory) {
      this(contextClass, new String[] { resourcePath }, parentFactory);
   }

   public BundleClassLoaderXmlBeanFactory(Class contextClass, String[] resourcePaths, BeanFactory parentFactory) {
      super(parentFactory);

      ClassLoader classLoader = contextClass.getClassLoader();
      reader.setBeanClassLoader(classLoader);
      Resource[] resources = new Resource[resourcePaths.length];

      for (int i = 0; i < resourcePaths.length; i++) {
         resources[i] = new ClassPathResource(resourcePaths[i], classLoader);
      }

      reader.loadBeanDefinitions(resources);
   }
}
